package net.kleinhenz.questor.model;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.core.Commit;

import net.kleinhenz.questor.exceptions.QuestorGameException;

public class Battle
{
	public enum Result {
		 PlayerWins, PlayerLoses, Draw, PlayerRetreats, FoeRetreats
	}
	
	@Attribute(required=false, name="condition")
	private String conditionText = null;
	
	@Attribute(required=false, name="conditiontype")
	private Executable.Type conditionType = null;

	@Attribute(required=true, name="foeid")
	private int foeId = -1;
	
	private BattleClass battleClass = null;
	private Condition condition = null;
	
	public BattleClass getBattleClass()
	{
		return battleClass;
	}

	public Condition getCondition()
	{
		return condition;
	}

	public void setCondition(Condition condition)
	{
		this.condition = condition;
	}

	public void setBattleClass(BattleClass battleClass)
	{
		this.battleClass = battleClass;
	}

	public void setConditionText(String conditionText) {
		this.conditionText = conditionText;
	}

	public String getConditionText() {
		return conditionText;
	}

	public void setConditionType(Executable.Type conditionType) {
		this.conditionType = conditionType;
	}

	public Executable.Type getConditionType() {
		return conditionType;
	}

	public void setFoeId(int foeId) {
		this.foeId = foeId;
	}

	public int getFoeId() {
		return foeId;
	}

	public Battle.Result fight(Quest quest) throws QuestorGameException
	{
		if (battleClass!=null)
			return battleClass.fight(quest.getPlayer(), quest.findFoe(getFoeId()));
		else throw new QuestorGameException("Error while performing battle: battleClass is null.");
	}
	
	@Commit
	public void createCondition() {
		if (getConditionType()!=null && getConditionText()!=null)
			setCondition(ConditionFactory.createCondition(getConditionType(), getConditionText()));
	}
}
